﻿// The MIT License (MIT)
// Copyright (c) 2014 Grégory Ghez
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


#if DEMO

namespace JenkinsController.Model
{
    using System;
    using System.Threading.Tasks;
    using Jenkins.Domain;

    internal class DemoDataService : IDataService
    {
        public async Task UpdateAsync(JenkinsServer server)
        {
            EventHandler start = this.StartLoading;
            if (start != null)
            {
                start(this, EventArgs.Empty);
            }

            var jobList = new[]
                          {
                              new JenkinsJob
                              {
                                  Url = "http://" + server.GetHashCode() + "/job/",
                                  Name = "Data Services",
                                  DisplayName = "Data Services",
                                  Status = new JenkinsJobStatus
                                           {
                                               State = JenkinsJobState.Success,
                                               IsRunning = false
                                           },
                                  LastBuild =
                                      new JenkinsBuild
                                      {
                                          Status = JenkinsBuildStatus.Success,
                                          Artifacts = new[]
                                                      {
                                                          new JenkinsArtifact { FileName = "webapp-1.3.2.zip" },
                                                          new JenkinsArtifact { FileName = "jenkins-lib-0.3.1.nupkg" },
                                                          new JenkinsArtifact { FileName = "dbupdate-1.3.2.zip" }
                                                      },
                                          IsBuilding = false,
                                          Number = 34,
                                          Culprits = new[] {  new JenkinsUserDetails { FullName = "John Doe" } },
                                          ChangeSet = new JenkinsChangeSet
                                                      {
                                                          Items = new[]
                                                                  {
                                                                      new JenkinsChangeSetItem
                                                                      {
                                                                          Author = new JenkinsUserDetails { FullName = "Grégory Ghez" },
                                                                          CommitId = "18676b907c18ce6e72506a2070edd6a2e1f4c3cd",
                                                                          Message = "Update version to 1.3.2",
                                                                          Changes = new[]
                                                                                    {
                                                                                        new JenkinsPathChange { ChangeType = "edit", FilePath = "src/Readme.txt" },
                                                                                        new JenkinsPathChange { ChangeType = "edit", FilePath = "src/VERSION" }
                                                                                    }
                                                                      },
                                                                      new JenkinsChangeSetItem
                                                                      {
                                                                          Author = new JenkinsUserDetails { FullName = "Grégory Ghez" },
                                                                          CommitId = "ed1f7b2cfc858623432edeae1f1862bac34acccd",
                                                                          Message = "[Fix] Invalid color link when status changed",
                                                                          Changes = new[]
                                                                                    {
                                                                                        new JenkinsPathChange { ChangeType = "edit", FilePath = "src/Domain/ColorLink.cs" },
                                                                                        new JenkinsPathChange { ChangeType = "edit", FilePath = "src/UI/JobDisplay.aspx.cs" }
                                                                                    }
                                                                      },
                                                                      new JenkinsChangeSetItem
                                                                      {
                                                                          Author = new JenkinsUserDetails { FullName = "Grégory Ghez" },
                                                                          CommitId = "ea2f9dcc62deb514a992041f837239a0432d8e73",
                                                                          Message = "Add Job display with status color",
                                                                          Changes = new[]
                                                                                    {
                                                                                        new JenkinsPathChange { ChangeType = "add", FilePath = "src/Domain/Job.cs" },
                                                                                        new JenkinsPathChange { ChangeType = "add", FilePath = "src/UI/JobDisplay.aspx.cs" }
                                                                                    }
                                                                      }
                                                                  },
                                                          SourceControllerKind = "git"
                                                      }
                                      },
                                  LastCompletedBuild = new JenkinsBuild { Duration = TimeSpan.FromSeconds(768d) },
                                  LastFailedBuild = new JenkinsBuild { TimeStamp = DateTime.Now.AddSeconds(-43d) },
                                  LastSuccessfulBuild = new JenkinsBuild { TimeStamp = DateTime.Now.AddHours(-1.23564d) }
                              },
                              new JenkinsJob
                              {
                                  Name = "Web User Interfaces",
                                  DisplayName = "Web User Interfaces",
                                  Url = "http://" + server.GetHashCode() + "/job/",
                                  Status = new JenkinsJobStatus
                                           {
                                               State = JenkinsJobState.Success,
                                               IsRunning = true
                                           },
                                  LastBuild = new JenkinsBuild
                                              {
                                                  IsBuilding = true,
                                                  Number = 56,
                                                  Culprits = new[] {  new JenkinsUserDetails { FullName = "John Doe" },new JenkinsUserDetails { FullName = "Grégory Ghez" } }
                                              },
                                  LastCompletedBuild = new JenkinsBuild { Duration = TimeSpan.FromSeconds(1562d) },
                                  LastFailedBuild = new JenkinsBuild { TimeStamp = DateTime.Now.AddDays(-5.254d) },
                                  LastSuccessfulBuild = new JenkinsBuild { TimeStamp = DateTime.Now.AddHours(-2.9854d) }
                              },
                              new JenkinsJob
                              {
                                  Name = "Web User Services",
                                  DisplayName = "Web User Services",
                                  Url = "http://" + server.GetHashCode() + "/job/",
                                  Status = new JenkinsJobStatus
                                           {
                                               State = JenkinsJobState.Success,
                                               IsRunning = false
                                           },
                                  LastBuild = new JenkinsBuild
                                              {
                                                  IsBuilding = false,
                                                  Number = 81,
                                                  Culprits = new[] {  new JenkinsUserDetails { FullName = "John Doe" },new JenkinsUserDetails { FullName = "Grégory Ghez" } }
                                              },
                                  LastCompletedBuild = new JenkinsBuild { Duration = TimeSpan.FromSeconds(762d) },
                                  LastFailedBuild = new JenkinsBuild { TimeStamp = DateTime.Now.AddDays(-18.254d) },
                                  LastSuccessfulBuild = new JenkinsBuild { TimeStamp = DateTime.Now.AddHours(-94.54d) }
                              },
                              new JenkinsJob
                              {
                                  Name = "Desktop Library API",
                                  DisplayName = "Desktop Library API",
                                  Url = "http://" + server.GetHashCode() + "/job/",
                                  Status = new JenkinsJobStatus
                                           {
                                               State = JenkinsJobState.Failed,
                                               IsRunning = false
                                           },
                                  LastBuild = new JenkinsBuild
                                              {
                                                  IsBuilding = true,
                                                  Number = 67,
                                                  TestReport = new JenkinsTestReport
                                                               {
                                                                   Duration = TimeSpan.FromSeconds(345),
                                                                   FailCount = 3,
                                                                   PassCount = 23,
                                                                   SkipCount = 1
                                                               },
                                                                Culprits = new[] { new JenkinsUserDetails { FullName = "Grégory Ghez" }, new JenkinsUserDetails { FullName = "John Doe" } }
                                              },
                                  LastCompletedBuild = new JenkinsBuild { Duration = TimeSpan.FromSeconds(1862d) },
                                  LastFailedBuild = new JenkinsBuild { TimeStamp = DateTime.Now.AddDays(-1.984d) },
                                  LastSuccessfulBuild = new JenkinsBuild { TimeStamp = DateTime.Now.AddHours(-1.5854d) }
                              },
                              new JenkinsJob
                              {
                                  Name = "Desktop Windows Service",
                                  DisplayName = "Desktop Windows Service",
                                  Url = "http://" + server.GetHashCode() + "/job/",
                                  Status = new JenkinsJobStatus
                                           {
                                               State = JenkinsJobState.Unstable,
                                               IsRunning = false
                                           },
                                  LastBuild = new JenkinsBuild
                                              {
                                                  IsBuilding = false,
                                                  Number = 12,
                                                  TestReport = new JenkinsTestReport
                                                               {
                                                                   Duration = TimeSpan.FromSeconds(845),
                                                                   FailCount = 1,
                                                                   PassCount = 36,
                                                                   SkipCount = 0
                                                               },
                                                  Culprits = new[] { new JenkinsUserDetails { FullName = "Grégory Ghez" } }
                                              },
                                  LastCompletedBuild = new JenkinsBuild { Duration = TimeSpan.FromSeconds(2452d) },
                                  LastFailedBuild = new JenkinsBuild { TimeStamp = DateTime.Now.AddDays(-2.144d) },
                                  LastSuccessfulBuild = new JenkinsBuild { TimeStamp = DateTime.Now.AddHours(-1.2144d) }
                              }
                          };

            server.Url = "http://" + server.GetHashCode() + "/";
            server.Node = new JenkinsNode
                          {
                              Description = "Server " + server.GetHashCode(),
                              Mode = "NORMAL",
                              NodeDescription = server.GetHashCode() + " node description",
                              NodeName = server.GetHashCode() + " node name",
                              NumExecutors = 4,
                              UseSecurity = false,
                              IsQuietingDown = false,
                              PrimaryView = new JenkinsView { Url = "http://" + server.GetHashCode() + "/view/" },
                              Views = new[]
                                      {
                                          new JenkinsView { Url = "http://" + server.GetHashCode() + "/view/", Name = "Develop", Jobs = jobList },
                                          new JenkinsView { Url = "http://" + server.GetHashCode() + "/view/", Name = "CTP", Jobs = jobList },
                                          new JenkinsView { Url = "http://" + server.GetHashCode() + "/view/", Name = "RC", Jobs = jobList },
                                          new JenkinsView { Url = "http://" + server.GetHashCode() + "/view/", Name = "Release", Jobs = jobList }
                                      },
                              Jobs = jobList,
                          };

            server.Users = new[]
                           {
                               new JenkinsUser
                               {
                                   Details = new JenkinsUserDetails { FullName = "Grégory Ghez" },
                                   Job = jobList[0],
                                   LastActivity = DateTime.Now.AddDays(-1.34)
                               },
                               new JenkinsUser
                               {
                                   Details = new JenkinsUserDetails { FullName = "John Doe" },
                                   Job = jobList[2],
                                   LastActivity = DateTime.Now.AddDays(-0.0057)
                               },
                               new JenkinsUser
                               {
                                   Details = new JenkinsUserDetails { FullName = "Richard Wallace" },
                                   Job = jobList[1],
                                   LastActivity = DateTime.Now.AddDays(-0.05414)
                               },
                               new JenkinsUser
                               {
                                   Details = new JenkinsUserDetails { FullName = "Walter" },
                                   Job = jobList[0],
                                   LastActivity = DateTime.Now.AddDays(-6.6541)
                               }
                           };

            EventHandler end = this.EndLoading;
            if (end != null)
            {
                end(this, EventArgs.Empty);
            }
        }

        public async Task UpdateAsync(JenkinsJob job)
        {
        }

        public async Task UpdateAsync(JenkinsBuild build)
        {
        }

        public async Task UpdateAsync(JenkinsView view)
        {
        }

        public async Task RunJobAsync(JenkinsJob job)
        {
        }

        public async Task StopJobAsync(JenkinsJob job)
        {
        }

        public event EventHandler StartLoading;
        public event EventHandler EndLoading;
    }
}

#endif